/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.commons.domain.buyer;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shop.cereshop.commons.config.RsaProperties;
import com.shop.cereshop.commons.utils.RsaUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.Pattern;
import java.io.Serializable;

/**
 * cere_buyer_user 客户信息实体类
 *
 * @author
 */
@Slf4j
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "CereBuyerUser", description = "客户信息实体类")
public class CereBuyerUser implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 客户id
     */
    @TableId(type = IdType.AUTO)
    private Long buyerUserId;

    /**
     * 脱敏之后的用户id
     */
    @TableField(exist = false)
    @JsonIgnore
    private String buyerUserIdMask;

    /**
     * 昵称
     */
    @ApiModelProperty(value = "昵称")
    @Pattern(regexp = "^[a-zA-Z0-9\\u4e00-\\u9fa5]{1,16}$", message = "昵称格式不正确")
    private String name;

    /**
     * 性别
     */
    @ApiModelProperty(value = "性别")
    private String sex;

    /**
     * 生日
     */
    @ApiModelProperty(value = "生日")
    private String birthday;

    @TableField(exist = false)
    @ApiModelProperty(value = "年龄")
    private Integer age;
    /**
     * 微信openID
     */
    @JsonIgnore
    @ApiModelProperty(value = "微信openID,无用字段")
    private String wechatOpenId;

    /**
     * 微信unionId
     */
    @JsonIgnore
    @ApiModelProperty(value = "微信unionId,无用字段")
    private String wechatUnionId;

    /**
     * 微信昵称
     */
    @JsonIgnore
    @ApiModelProperty(value = "微信昵称,无用字段")
    private String wechatName;

    /**
     * 微信号
     */
    @JsonIgnore
    @ApiModelProperty(value = "微信号,无用字段")
    private String wechatNumber;

    /**
     * 支付宝小程序用户id
     */
    @JsonIgnore
    @ApiModelProperty(value = "支付宝小程序用户id,无用字段")
    private String aliUserId;

    /**
     * 注册手机号
     */
    @ApiModelProperty(value = "注册手机号")
    private String phone;

    /**
     * 密码
     */
//    @JsonIgnore
    @JsonProperty(access = JsonProperty.Access.READ_WRITE)
    @ApiModelProperty(value = "密码")
    private String password;

    /**
     * 头像图片
     */
    @ApiModelProperty(value = "头像图片")
    private String headImage;

    /**
     * 背景图片
     */
    @ApiModelProperty(value = "背景图片")
    private String backgroundImage;

    /**
     * 是否启用   1-是 0-否
     */
    private Integer state;

    /**
     * 是否加入黑名单 1-是 0-否
     */
    private Integer ifBlack;

    /**
     * 备注
     */
    @ApiModelProperty(value = "个人简介")
    private String remark;

    /**
     * 请求token
     */
    @JsonIgnore
    private String token;

    /**
     * 会员等级id
     */
    private Long memberLevelId;

    /**
     * 会员等级名称
     */
    @TableField(exist = false)
    private String memberLevelName;

    /**
     * 成长值
     */
    private Integer growth;

    /**
     * 积分
     */
    private Integer credit;

    /**
     * 注册来源 0-未知 1-APP 2-小程序 3-H5 4-支付宝小程序 5-PC
     */
    @JsonIgnore
    @ApiModelProperty(value = "注册来源 0-未知 1-APP 2-小程序 3-H5 4-支付宝小程序 5-PC")
    private Integer terminal;

    /**
     * 下一级需要的成长值
     */
    @TableField(exist = false)
    private Integer nextLevelGrowth;

    /**
     * 下一级会员等级名称
     */
    @TableField(exist = false)
    private String nextLevelName;

    @ApiModelProperty(value = "注册时的ip")
    private String registerIp;

    @ApiModelProperty(value = "上次登录的ip")
    private String lastLoginIp;

    @JsonIgnore
    @ApiModelProperty(value = "渠道编码")
    private String channelCode;

    /**
     * 创建时间
     */
    private String createTime;

    /**
     * 更新时间
     */
    private String updateTime;



    /**
     * 六位用户账号
     */
    @ApiModelProperty(value = "用户六位非靓号的账号")
    private String userAccount;

    /**
     * 客户二维码
     */
    @ApiModelProperty(value = "用户二维码")
    private String userQrCode;

    /**
     * 用户所在省份
     */
    @ApiModelProperty(value = "用户所在省份")
    private String province;
    /**
     * 用户所在城市
     */
    @ApiModelProperty(value = "用户所在城市")
    private String city;


    @ApiModelProperty(value = "是否展示附近")
    private String isShowNearby;

    @TableField(exist = false)
    @ApiModelProperty(value = "认证状态：0未认证，1审核中，2已认证，3认证失败")
    private Integer authStatus;

    @ApiModelProperty(value = "是否允许发布动态 1-是,0-否")
     private  Integer ifAllowTrends;

    @ApiModelProperty(value = "是否允用户启用Im 1-是,0-否")
     private  Integer ifAllowIm;
    @ApiModelProperty(value = "是否允许发布品论,收藏，点赞，关注 1-是 0否")
    private Integer ifAllowOther;

    /**
     * RSA解密
     *
     * @return 解密对象
     * @throws Exception
     */
    public CereBuyerUser decrypt() throws Exception {
        CereBuyerUser res = this;
        log.info("解密前密码: {}", this.password);
        if (StrUtil.isNotBlank(this.password)) {
            res.setPassword(RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, this.password));
            log.info("解密后密码: {}", res.getPassword());
        }
        if (StrUtil.isNotBlank(this.phone)) {
            res.setPhone(RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, this.phone));
        }
        return res;
    }
}
